﻿using Model;
using System;
using System.Collections.Generic;
using System.Data.Entity.Migrations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
   public class ProductService
    {
        public static List<Product> GetProducts()
        {
            using (ShopDemoContextt db = new ShopDemoContextt())
            {
                return db.Product .Where(p=>p.Isstate=="上架").ToList();
            }
        }
        public static List<Product> GetProductMes(int limit, int page, string ProductName, string Isstate, out int total)
        {
            using (ShopDemoContextt db = new ShopDemoContextt())
            {
                var query = db.Product.Where(p => true);
                if (!string.IsNullOrEmpty(ProductName))
                {
                    query = query.Where(p => p.ProductName.Contains(ProductName));
                }
                if (!string.IsNullOrEmpty(Isstate))
                {
                    query = query.Where(p => p.Isstate== Isstate);
                }
                total = query.Count();
                return query.OrderBy(p => p.Id).Skip((page - 1) * limit).Take(limit).ToList();
            }
        }


        /// <summary>
        /// 根据id获取数据
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public static Product GetProductId(int Id)
        {
            using (ShopDemoContextt db = new ShopDemoContextt())
            {
                return db.Product.FirstOrDefault(p=>p.Id==Id);
            }
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public static int GetBatchDelByIds(string Ids)
        {
            using (ShopDemoContextt db = new ShopDemoContextt())
            {
                string newids = Ids.Substring(0, Ids.Length - 1);
                string sql = $"delete [Product] where Id in ({newids})";
                return db.Database.ExecuteSqlCommand(sql);
            }

        }
        /// <summary>
        /// 批量上架
        /// </summary>
        /// <param name="Ids"></param>
        /// <returns></returns>
        public static int FaBu(string Ids)
        {
            using (ShopDemoContextt db = new ShopDemoContextt())
            {
                string newids = Ids.Substring(0, Ids.Length - 1);
                string sql = $"Update Product set Isstate = '上架' where Id in ({newids})";
                return db.Database.ExecuteSqlCommand(sql);
            }
        }
        /// <summary>
        /// 批量下架
        /// </summary>
        /// <param name="Ids"></param>
        /// <returns></returns>
        public static int XiaJia(string Ids)
        {
            using (ShopDemoContextt db = new ShopDemoContextt())
            {
                string newids = Ids.Substring(0, Ids.Length - 1);
                string sql = $"Update Product set Isstate = '下架' where Id in ({newids})";
                return db.Database.ExecuteSqlCommand(sql);
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static int GetDel(int id)
        {
            using (ShopDemoContextt db = new ShopDemoContextt())
            {
                Product product = new Product() { Id = id };
                db.Product.Attach(product);
                db.Product.Remove(product);
                return db.SaveChanges();
            }
        }
        /// <summary>
        /// 添加商品
        /// </summary>
        /// <param name="product"></param>
        /// <returns></returns>
        public static int GetAdd(Product product)
        {
            using (ShopDemoContextt db = new ShopDemoContextt())
            {
                db.Product.Add(product);
                return db.SaveChanges();
            }
        }

        public  static int GetUpdate(Product product)
        {
            using (ShopDemoContextt db = new ShopDemoContextt())
            {
                Product product1 = db.Product.SingleOrDefault(p => p.Id == product.Id);
                product1.ProductName = product.ProductName;
                product1.ProductImg = product.ProductImg;
                product1.Price = product.Price;
                product1.Count = product.Count;
                product1.Particulars = product.Particulars;
                product1.Site = product.Site;
                product1.CreateTime = product.CreateTime;
                product1.TypeId = product.TypeId;
                product1.Isstate = product.Isstate;
                //db.Product.AddOrUpdate(product);
                return db.SaveChanges();
            }
        }


    }
}
